package Conexion;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Conexion
{
    public static Connection con;
    public static boolean loggedIn = false;
    private static Conexion ConexionConectada = null;    
    protected ResultSet rs;
    private String usr,pass,baseDatos,puerto,ip;
    

    public Conexion()
    {
        this.usr = "externo";
        this.pass = "oracle123";
        this.baseDatos="broker";
        this.puerto="1521";
        this.ip="192.168.0.102";
        ifConnected();
    } 
    
    public PreparedStatement statamet(String sql) throws SQLException
    {
        return con.prepareStatement(sql);
    }
    
    private void ifConnected()
    {
        try
        {
            if (Conexion.loggedIn == true)
            {
                con = Conexion.getDatabaseConnection().getConnection();
            }
            else
            {
                Conectar();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    public Savepoint SavePoint() throws SQLException
    {
        return con.setSavepoint();
    }

    public static Conexion getDatabaseConnection()
    {
        return Conexion.ConexionConectada;
    }

    private static void setDatabaseConnection(Conexion conexion)
    {
        Conexion.ConexionConectada = conexion;
    }

        
    public Connection getConnection()
    {
        return con;
    }
    
    public void Transacction()
    {
        try 
        {
            con.setAutoCommit(false);
        }
        catch (SQLException ex) 
        {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void commit()
    {
        try 
        {
            con.commit();
        }
        catch (SQLException ex) 
        {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
  
    
    public void rollback()
    {
        try 
        {
            con.rollback();
        }
        catch (SQLException ex) 
        {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void rollback(Savepoint savepoint)
    {
        try 
        {
            con.rollback(savepoint);
        }
        catch (SQLException ex) 
        {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    
    
    public void EjecutarSentencia(String sentencia) throws SQLException
    {
        ifConnected();
        PreparedStatement ps;
        ps = con.prepareStatement(sentencia);
        ps.execute();
    }
    
    public void EjecutarUpdate(String sentencia)
    {
        ifConnected();
        PreparedStatement ps;
        try
        {
            ps = con.prepareStatement(sentencia);
            ps.executeUpdate();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
    
    public void Close()
    {
        try
        {
            loggedIn=false;
            con.close();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    public ResultSet EjecutarConsulta(String consulta)
    {
        ifConnected();
        PreparedStatement ps;
        try
        {
            ps = con.prepareStatement(consulta);
            rs = ps.executeQuery();
            return rs;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            return null;
        }
    }

    public void Conectar()
    {
        try
        {
            //SQL SERVER Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
            Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); //Oracle
            // MySQL Class.forName("com.mysql.jdbc.Driver").newInstance();
            Login();

        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (Exception exc)
        {
            exc.printStackTrace();
        }
    }

    private void Login()
    {        
        try
        {
            //String url = "jdbc:mysql://s156.eatj.com:3306/ruddypazde";
            //con = DriverManager.getConnection(url, "ruddypazde", "RRPdrjhsad");            
            //con = DriverManager.getConnection("jdbc:sqlserver://192.168.1.102:1433;databaseName=apuestasMundial", "sa", "RRPdrjhsad2014*");
            con = DriverManager.getConnection("jdbc:oracle:thin:@"+ip+":"+puerto+":"+baseDatos, usr, pass);
            
            Conexion.loggedIn = true;
            Conexion.setDatabaseConnection(this);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }        
    //public JasperPrint reportePDF(Map parametros,String reporte ,HttpServletRequest request, HttpSession session){

    public static boolean isLoggedIn() {
        return loggedIn;
    }

    public static void setLoggedIn(boolean loggedIn) {
        Conexion.loggedIn = loggedIn;
    }

    public ResultSet getRs() {
        return rs;
    }

    public void setRs(ResultSet rs) {
        this.rs = rs;
    }

    public String getUsr() {
        return usr;
    }

    public void setUsr(String usr) {
        this.usr = usr;
    }

    public String getBaseDatos() {
        return baseDatos;
    }

    public void setBaseDatos(String baseDatos) {
        this.baseDatos = baseDatos;
    }

    public String getPuerto() {
        return puerto;
    }

    public void setPuerto(String puerto) {
        this.puerto = puerto;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }
    

}
